#pragma once
// 文件版本
#include "../comm/util.hpp"
#include "../comm/log.hpp"

#include <unordered_map>
#include <vector>
#include <cassert>
#include <fstream>
#include <algorithm>

namespace ns_model
{
    using namespace std;
    using namespace ns_util;
    using namespace ns_log;

    struct Question
    {
        string number; // 题号
        string title;  // 名称
        string star;   // 难度: 简单 中等 困难
        int cpu_limit; // 时间要求(S)
        int mem_limit; // 空间要求(KB)
        string desc;   // 题目描述
        string header; // 用户提交代码
        string tail;   // 测试代码
    };

    const string questins_list = "./questions/questions.list";
    const string questins_path = "./questions/";

    class Model
    {
    private:
        unordered_map<string, Question> questions;

    public:
        Model()
        {
            assert(LoadQuestionList(questins_list));
        }
        bool LoadQuestionList(const string &question_list)
        {
            ifstream in(questins_list);
            if (!in.is_open())
            {
                LOG(FATAL) << "题库加载失败" << endl;
                return false;
            }

            string line;
            while (getline(in, line))
            {
                vector<string> tokens;
                StringUtil::SpiltString(line, &tokens, " ");
                if (tokens.size() != 5)
                {
                    LOG(ERROR) << "部分题目加载失败" << endl;
                    continue;
                }
                Question q;
                q.number = tokens[0];
                q.title = tokens[1];
                q.star = tokens[2];
                q.cpu_limit = stoi(tokens[3]);
                q.mem_limit = stoi(tokens[4]);
                string path = questins_path + q.number + "/";
                FileUtil::ReadFile(path + "desc.txt", q.desc, true);
                FileUtil::ReadFile(path + "header.cpp", q.header, true);
                FileUtil::ReadFile(path + "tail.cpp", q.tail, true);
                questions.insert({q.number, q});
            }

            LOG(INFO) << "加载题库...成功!" << "\n";
            in.close();

            return true;
        }
        bool GetAllQuestion(vector<Question> *out)
        {
            if (questions.size() == 0)
            {
                LOG(ERROR) << "用户获取题库失败" << endl;
                return false;
            }
            for (auto &it : questions)
            {
                out->push_back(it.second);
            }
            sort(out->begin(), out->end(), [](const struct Question &q1, const struct Question &q2)
                 { return stoi(q1.number) < stoi(q2.number); });
            LOG(DEBUG) << "题库加载成功" << endl;
            return true;
        }
        bool GetOneQuestion(const string &number, Question *q)
        {
            auto iter = questions.find(number);
            if (iter == questions.end())
            {
                LOG(ERROR) << "指定题目获取失败, 题目编号: " << number << endl;
                return false;
            }
            (*q) = iter->second;
            LOG(DEBUG) << "指定题目获取成功, 题目编号: " << number << endl;
            return true;
        }
    };
}
